package com.neusoft.elmboot.controller;

import java.util.List;

import com.neusoft.elmboot.dto.OrderDTO;
import com.neusoft.elmboot.dto.UserDTO;
import com.neusoft.elmboot.po.OrderDetailet;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.ex.DataNotFoundException;
import com.neusoft.elmboot.service.ex.NotEnoughException;
import com.neusoft.elmboot.service.ex.PaymentException;
import com.neusoft.elmboot.service.ex.UpdateException;
import com.neusoft.elmboot.util.JsonResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.neusoft.elmboot.po.Orders;
import com.neusoft.elmboot.service.OrdersService;

@RestController
@RequestMapping("/orders")
public class OrdersController {
	public static final int OK = 200;
	public static final int ERROR = 500;
	public static final int DATA_NOT_FOUND = 204;

	@Autowired
	private OrdersService ordersService;

	@PostMapping
	public JsonResult<OrderDTO> createOrders(@RequestBody OrderDTO orderDTO) throws Exception{
		JsonResult<OrderDTO> result = new JsonResult<>();
		if (orderDTO == null) {
			result.setState(ERROR);
			result.setMessage("传过来的订单数据不能为空！");
			return result;
		}
		Orders orders = new Orders();
		orders.setUserId(orderDTO.getUserId());
		orders.setBusinessId(orderDTO.getBusinessId());
		orders.setDaId(orderDTO.getDaId());
		try {
			int orderId = ordersService.createOrders(orders);
			orderDTO.setOrderId(orderId);
			result = new JsonResult<>(OK, orderDTO);
			result.setMessage("订单创建成功");
			return result;
		}catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}catch(Exception e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@GetMapping("/orderDetail/{orderId}")
	public JsonResult<List<OrderDetailet>> getOrderDetailById(@PathVariable Integer orderId){
		JsonResult<List<OrderDetailet>> result = new JsonResult<List<OrderDetailet>>();
		try {
			List<OrderDetailet> data = ordersService.getOrderDetailById(orderId);
			result = new JsonResult<List<OrderDetailet>>(OK, data);
			result.setMessage("获取订单明细成功！");
			return result;
		}catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	@GetMapping("/{orderId}")
	public JsonResult<Orders> getOrdersById(@PathVariable Integer orderId) throws Exception{
		JsonResult<Orders> result = new JsonResult<Orders>();
		try {
			Orders data = ordersService.getOrdersById(orderId);
			result = new JsonResult<Orders>(OK, data);
			result.setMessage("获取订单成功");
			return result;
		}catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@GetMapping("/user/{userId}")
	public JsonResult<List<Orders>> listOrdersByUserId(@PathVariable String userId) throws Exception{
		JsonResult<List<Orders>> result = new JsonResult<List<Orders>>();
		try {
			List<Orders> data = ordersService.listOrdersByUserId(userId);
			result = new JsonResult<List<Orders>>(OK, data);
			result.setMessage("获取用户订单列表成功");
			return result;
		}
		catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@PutMapping("/payment/{orderId}")
	public JsonResult<OrderDTO> payOrders(@PathVariable Integer orderId){
		JsonResult<OrderDTO> result = new JsonResult<OrderDTO>();
		OrderDTO orderDTO = new OrderDTO();
		Orders orders = ordersService.getOrdersById(orderId);
		orderDTO.setBusinessId(orders.getBusinessId());
		orderDTO.setDaId(orders.getDaId());
		orderDTO.setUserId(orders.getUserId());
		try {
			int data = ordersService.payOrders(orderId);
			result = new JsonResult<OrderDTO>(OK, orderDTO);
			result.setMessage("订单支付成功！");
			return result;
		}catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}catch (UpdateException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}catch (PaymentException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@PutMapping("/cancel/{orderId}")
	public JsonResult<OrderDTO> cancelOrders(@PathVariable Integer orderId){
		JsonResult<OrderDTO> result = new JsonResult<OrderDTO>();
		OrderDTO orderDTO = new OrderDTO();
		Orders orders = ordersService.getOrdersById(orderId);
		orderDTO.setBusinessId(orders.getBusinessId());
		orderDTO.setDaId(orders.getDaId());
		orderDTO.setUserId(orders.getUserId());
		try {
			int data = ordersService.cancelOrders(orderId);
			result = new JsonResult<OrderDTO>(OK, orderDTO);
			result.setMessage("订单取消成功！");
			return result;
		}catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}catch (UpdateException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@DeleteMapping("/{orderId}")
	public JsonResult<OrderDTO> deleteOrders(@PathVariable Integer orderId){
		JsonResult<OrderDTO> result = new JsonResult<OrderDTO>();
		OrderDTO orderDTO = new OrderDTO();
		Orders orders = ordersService.getOrdersById(orderId);
		orderDTO.setBusinessId(orders.getBusinessId());
		orderDTO.setDaId(orders.getDaId());
		orderDTO.setUserId(orders.getUserId());
		try {
			int data = ordersService.deleteOrders(orderId);
			result = new JsonResult<OrderDTO>(OK, orderDTO);
			result.setMessage("订单删除成功！");
			return result;
		}catch (DataNotFoundException e) {
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}catch (UpdateException e) {
			result.setState(ERROR);
			result.setMessage(e.getMessage());
			return result;
		}
	}
}
